// pages/divide/order/index.js
const app = getApp()
const ip=app.globalData.ip
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    userId:'',
    storeId:'',
    localId:'',
    mapjingdu:'',
    maoweidu:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    // this.setData({
    //   localId:options.id
    // })
    if(options.id!=null){
      this.selectOrderDetails(options.id)
    }else{
      this.selectOrderDetails(options)
    }
    
    wx.getLocation({
      success:res=>{
        console.log(res)
        this.setData({
          mapjingdu:res.latitude,
          maoweidu:res.longitude
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.selectOrderDetails(this.data.localId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectOrderDetails(id){
    wx.request({
      url: ip+'/StoreToAssignment/selectOrderDetails',
      method:'GET',
      data:{
        assignmentId:id
      },
      success:res=>{
        console.log(res)
        this.setData({

          order:res.data.data,
          storeId:wx.getStorageSync('storeid'),
          userId:wx.getStorageSync('userid'),
        });
      }
    })
  },
  //取消后的商家接单按钮
  divideBtn(res){
    console.log("1")
  },
  //拒绝接单
  cancelOrderBtn(res){
    console.log(res.currentTarget.dataset.assignment)
    wx.request({
      url: ip+'/StoreToAssignment/cancelOrderBtn',
      data:{
        assignmentId:res.currentTarget.dataset.assignment,
        storeId:wx.getStorageSync('storeid')
      },
      method:"GET",
      success:e=>{
        console.log(e);
        this.onLoad(res.currentTarget.dataset.assignment)
        wx.showToast({
          title: '操作成功！', // 标题
          icon: 'success',  // 图标类型，默认success
          duration: 40000,  // 提示窗停留时间，默认1500ms
          success(){
            wx.switchTab({
              url: '../../userCenter/index',
            })
          }
        })
      }
    })
  },
  //商家接单按钮
  accessOrderBtn(res){
    console.log(res.currentTarget.dataset.assignment)
    wx.request({
      url: ip+'/StoreToAssignment/accessOrderBtn',
      data:{
        assignmentId:res.currentTarget.dataset.assignment,
        storeId:wx.getStorageSync('storeid')
      },
      method:"GET",
      success:e=>{
        console.log(e);
        this.onLoad(res.currentTarget.dataset.assignment)
      }
    })
  },
  //商家接受取消的订单
  cancelButWantOrderBtn(res){
    console.log(res.currentTarget.dataset.assignment)
    wx.request({
      url: ip+'/StoreToAssignment/cancelButWantOrderBtn',
      data:{
        assignmentId:res.currentTarget.dataset.assignment,
        storeId:wx.getStorageSync('storeid')
      },
      method:"GET",
      success:e=>{
        console.log(e);
        if(e.data.data==null){
          wx.showToast({
            title: e.data.msg,
            icon:'none'
          })
        }
        this.onLoad(res.currentTarget.dataset.assignment)
      }
    })
  },
  //商家点击确认到达按钮
  accessArriveBtn(res){
    console.log(res.currentTarget.dataset.assignment)
    wx.request({
      url: ip+'/StoreToAssignment/accessArriveBtn',
      data:{
        assignmentId:res.currentTarget.dataset.assignment,
          latitude:this.data.mapjingdu,
          longitude:this.data.maoweidu,
        storeId:wx.getStorageSync('storeid')
      },
      method:"GET",
      success:e=>{
        console.log(e);
        if(e.data.data==null){
          wx.showToast({
            title: e.data.msg,
            icon: 'none'
          })
        }
        this.onLoad(res.currentTarget.dataset.assignment)
      }
    })
  },
  //用户确认商家到达按钮
  accessArriveBtnUser(res){
    console.log(res.currentTarget.dataset.assignment)
    wx.request({
      url: ip+'/StoreToAssignment/accessArriveBtnUser',
      data:{
        assignmentId:res.currentTarget.dataset.assignment,
        //storeId:wx.getStorageSync('storeid')
        userId:wx.getStorageSync('userid')
      },
      method:"GET",
      success:e=>{
        console.log(e);
        this.onLoad(res.currentTarget.dataset.assignment)
      }
    })
  },
  //用户确认商家完成
  accessSuccessBtnUser(res){
    console.log(res.currentTarget.dataset.assignment)
    wx.request({
      url: ip+'/StoreToAssignment/accessSuccessBtnUser',
      data:{
        assignmentId:res.currentTarget.dataset.assignment,
        //storeId:wx.getStorageSync('storeid')
        userId:wx.getStorageSync('userid')
      },
      method:"GET",
      success:e=>{
        console.log(e);
        this.onLoad(res.currentTarget.dataset.assignment)
      }
    })
  },
  //评论
  discussBtnUser(res){
    console.log(res);
    wx.navigateTo({
      url: '../../userCenter/evaluate/index?assignmentId='+res.currentTarget.dataset.assignment+"&storeid="+res.currentTarget.dataset.storeid,
    })
  },
  //查看评论
  selectDiscussUser(res){
    console.log(res);
    wx.navigateTo({
      url: '../../userCenter/evaluate/index?assignmentId='+res.currentTarget.dataset.assignment+"&storeid="+res.currentTarget.dataset.storeid,
    })
  },
  getMap(){
    wx.getLocation({
      success:res=>{
        console.log(res)
        let plugin = requirePlugin('routePlan');
let key ='456BZ-4YAER-CQQW4-WU6ST-3KHKT-DPFVJ'; //使用在腾讯位置服务申请的key
let referer ='xiangpai';  //调用插件的小程序的名称
let startPoint = JSON.stringify({ //起点
  'name':'中国技术交易大厦',
  'latitude': res.latitude,
  'longitude': res.longitude
});
let endPoint = JSON.stringify({ //终点
  'name':this.data.order.shootingAddress,
  'latitude': this.data.order.latitude,
  'longitude': this.data.order.longitude
});
wx.navigateTo({
  url:'plugin://routePlan/route-plan?key=' + key +'&referer=' + referer +'&endPoint=' + endPoint
});
      }
    })

  }
})